package com.thesis.client;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.thesis.object.ListTable;

public class TableAdapter extends BaseAdapter {
	private Context context;
	private final ListTable listtable;
	private final int[] role;

	public TableAdapter(Context context, ListTable listtable, int[] role) {
		this.context = context;
		this.listtable = listtable;
		this.role = role;
	}

	public ImageView imageView_table;

	public View getView(int position, View convertView, ViewGroup parent) {

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View gridView;

		if (convertView == null) {

			gridView = new View(context);

			// get layout from mobile.xml
			gridView = inflater.inflate(R.layout.table_grid, null);

			// set value into textview
			// TextView textView = (TextView) gridView
			// .findViewById(R.id.grid_item_label);
			// textView.setText(table[position]);
			//
			// // set image based on selected text
			// imageView_table = (ImageView) gridView
			// .findViewById(R.id.grid_item_image);
			// if (check[position] == 0)
			// imageView_table.setImageResource(R.drawable.table_available);
			// else if (check[position] == 2)
			// imageView_table.setImageResource(R.drawable.table_no);
			// else imageView_table.setImageResource(R.drawable.table_book);

		} else {
			gridView = (View) convertView;
		}

		TextView textView = (TextView) gridView
				.findViewById(R.id.grid_item_label);

		imageView_table = (ImageView) gridView
				.findViewById(R.id.grid_item_image);
		
		if(listtable.getListTable().get(position).getBookingStatusId() == 4){
			textView.setText(listtable.getListTable().get(position).getTableName()+"-B");
		}
		else {
			textView.setText(listtable.getListTable().get(position).getTableName());
		}
		

		if (role[0] == 0) {
//			textView.setText(listtable.getListTable().get(position)
//					.getTableName());
			if (listtable.getListTable().get(position).getManagerId() == role[2]) {
				if (listtable.getListTable().get(position).getStatusId() == 1)
					imageView_table.setImageResource(R.drawable.table_avai);

				else if (listtable.getListTable().get(position).getStatusId() == 2)
					imageView_table.setImageResource(R.drawable.table_serve);

				else {
					imageView_table.setImageResource(R.drawable.table_extra);

				}
			} else {
				imageView_table.setImageResource(R.drawable.table_no);
			}

		} else {
			if (listtable.getListTable().get(position).getManagerId() == role[1]) {
				if (listtable.getListTable().get(position).getStatusId() == 1) {
					imageView_table.setImageResource(R.drawable.table_avai);
//					textView.setText(listtable.getListTable().get(position)
//							.getTableName());
				} else if (listtable.getListTable().get(position).getStatusId() == 2) {
					imageView_table.setImageResource(R.drawable.table_serve);
//					textView.setText(listtable.getListTable().get(position).getTableName()
//							);
				} else {
					imageView_table.setImageResource(R.drawable.table_extra);
//					textView.setText(listtable.getListTable().get(position).getTableName()
//							);
				}
			} else {
				textView.setText(listtable.getListTable().get(position)
						.getTableName());
				imageView_table.setImageResource(R.drawable.table_no);
			}
		}

		return gridView;
	}

	@Override
	public int getCount() {
		return listtable.getListTable().size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

}